/**
 * 隐患上报 编辑页
 *
 * 作者 wanghw
 * 日期 2024-08-20 09:23:04
 */
layui.use(['jquery', 'febs', 'form', 'validate', 'fileUpload',  'autocomplete', 'commonJS'], function () {
    var $ = layui.$,
        febs = layui.febs,
        fileUpload = layui.fileUpload,
        layer = layui.layer,
        form = layui.form,
        $view = $('#hazardReportAudit-edit'),
        $unsafeName = $('#unsafeName'),
        commonJS = layui.commonJS,
        validate = layui.validate;

    form.verify(validate);
    form.render();

    initFormValue();




    function initFormValue() {
        initImg();
        initDetailTale();
        febs.initDictItemLabel($unsafeName, 'SAFETY_HAZARD_UNSAFE_FORM', hazardReport.unsafeName);
    }

    function initImg() {

        var fileUploadDiv = fileUpload.render({
            elem: '#haazardFiles',
            url: ctx + 'hazard/upload',
            type: 0,//0-图片，1-视频，2-其他
            maxNum: 5,
            defaultData: hazardReport && hazardReport.fileList ? hazardReport.fileList : [],
            isDetail: true,
            done: function (res) {
            },
        });

        var fileUploadDiv = fileUpload.render({
            elem: '#feedBackImgView',
            url: ctx + 'hazard/upload',
            type: 0,//0-图片，1-视频，2-其他
            maxNum: 5,
            defaultData: hazardReport && hazardReport.feedBackFileList ? hazardReport.feedBackFileList : [],
            isDetail: true,
            done: function (res) {
            },
        });

    }

    form.on('submit(hazardReportAudit-edit-form-submit)', function (data) {
        if (data.field.nearmissFlg == 'on') {
            data.field.nearmissFlg = '1';
        } else {
            data.field.nearmissFlg = '0';
        }

        if (data.field.status == '13') {
            data.field.auditRemark = data.field.auditReason;
        }
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        febs.modal.confirm('保存审核信息', '确定提交信息？', function () {
            febs.postArray(ctx + 'hazardReportRectificationAudit', data, function () {
                layer.closeAll();
                febs.alert.success('保存成功');
                $('#febs-hazardReportAudit').find('#query').trigger('click');
            });
        });

    }


    form.on('radio(auditStatus)', function (obj) {
        if (obj.value == '6') {
            //审核
            $view.find('#auditInfo').removeClass('febs-hide');
            $view.find('#auditRebackInfo').addClass('febs-hide');
            $view.find('textarea[name="auditReason"]').attr("lay-verify", "");

            $view.find('#audiRemarkLabel').removeClass("febs-form-item-require");
        } else {
            //审核退回
            $view.find('#auditInfo').addClass('febs-hide');
            $view.find('#auditRebackInfo').removeClass('febs-hide');
            $view.find('textarea[name="auditReason"]').attr("lay-verify", "required")

            $view.find('#audiRemarkLabel').removeClass("febs-form-item-require");
        }

    })

    function initDetailTale(){
        if(hazardReport.planRecordList){
            planTableIns = febs.table.init({
                elem: $view.find('table[id="hazardReportPlanRecordTable"]'),
                id: 'hazardReportPlanRecordTable',
                page:false,
                skin: '',
                limit:Number.MAX_VALUE,
                cols: [
                    [
                        {type: 'numbers',title:'序号'},
                        {field: 'auditRealName', title: '制定人姓名', width: 120},
                        {
                            field: 'auditResult', title: '制定结果', width: 100, align: 'center', templet: function (d) {
                                if (d.auditResult == '1') {
                                    return '<span class="layui-badge febs-tag-green">通过</span>';
                                } else {
                                    return '<span class="layui-badge febs-tag-magenta">退回</span>';
                                }
                            }
                        },
                        {field: 'createdTime', title: '制定时间', width: 160,align:'center'},
                        {field: 'auditReason', title: '制定理由', minWidth: 120 }
                    ]
                ],
                data:hazardReport.planRecordList
            });
        }

        if(hazardReport.feedBackRecordList){
            feedBackTableIns = febs.table.init({
                elem: $view.find('table[id="hazardReportFeedBackRecordTable"]'),
                id: 'hazardReportFeedBackRecordTable',
                page:false,
                skin: '',
                limit:Number.MAX_VALUE,
                cols: [
                    [
                        {type: 'numbers',title:'序号'},
                        {field: 'auditRealName', title: '反馈人姓名',width: 120},
                        {
                            field: 'auditResult', title: '反馈结果', width: 100, align: 'center', templet: function (d) {
                                if (d.auditResult == '1') {
                                    return '<span class="layui-badge febs-tag-green">通过</span>';
                                } else {
                                    return '<span class="layui-badge febs-tag-magenta">退回</span>';
                                }
                            }
                        },
                        {field: 'createdTime', title: '反馈时间', width: 160,align:'center'},
                        {field: 'auditReason', title: '反馈理由', minWidth: 120 },
                    ]
                ],
                data:hazardReport.feedBackRecordList
            });
        }

        if(hazardReport.auditRecordList){
            feedBackTableIns = febs.table.init({
                elem: $view.find('table[id="hazardReportAuditRecordTable"]'),
                id: 'hazardReportAuditRecordTable',
                page:false,
                skin: '',
                limit:Number.MAX_VALUE,
                cols: [
                    [
                        {type: 'numbers',title:'序号'},
                        {field: 'auditRealName', title: '审核人姓名', width: 120},
                        {
                            field: 'auditResult', title: '审核结果', width: 100, align: 'center', templet: function (d) {
                                if (d.auditResult == '1') {
                                    return '<span class="layui-badge febs-tag-green">审核通过</span>';
                                } else {
                                    return '<span class="layui-badge febs-tag-magenta">审核退回</span>';
                                }
                            }
                        },
                        {field: 'createdTime', title: '审核时间', width: 160,align:'center'},
                        {field: 'auditReason', title: '审核理由', minWidth: 120 },
                    ]
                ],
                data:hazardReport.auditRecordList
            });
        }
        form.render()
    }


});